package com.syxp.pda.action.base;

import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.Department;
import com.syxp.pda.entity.base.Station;
import com.syxp.pda.service.base.StationService;

public class StationAction extends BaseAction {
	@Resource
	private StationService stationService;

	public StationService getStationService() {
		return stationService;
	}

	public void setStationService(StationService stationService) {
		this.stationService = stationService;
	}
	
	private int stationID;//传入的参数
	private Station station; //注入Station实体类
	
	public int getStationID() {
		return stationID;
	}

	public void setStationID(int stationID) {
		this.stationID = stationID;
	}

	public Station getStation() {
		return station;
	}

	public void setStation(Station station) {
		this.station = station;
	}
	
	public String save(){ //保存
		try{
			System.out.println(station) ;
			Station s= stationService.addService(station) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("station.stationID", String.valueOf(station.getStationID())) ;
			jobj.addProperty("station.stationCode", station.getStationCode()) ;
			jobj.addProperty("station.stationName", station.getStationName()) ;
			jobj.addProperty("station.remarks", station.getRemarks()) ;
			jobj.addProperty("success", true) ;
			this.jsonString=gson.toJson(jobj) ;
		}catch(Exception e) {
			e.printStackTrace() ;
		}
		return SUCCESS ;
	}
	
	public String load() {  //加载所有数据
		QueryFilter filter = new QueryFilter() ;
		List<Station> all = stationService.getDao().getAll(filter);
		JsonArray jarray = new JsonArray() ;
		for(Station station : all) {
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("stationCode", station.getStationCode()) ;
			jobj.addProperty("stationName", station.getStationName()) ;
			jobj.addProperty("remarks", station.getRemarks()) ;
			jobj.addProperty("stationID", String.valueOf(station.getStationID())) ;
			jarray.add(jobj) ;
		}
		this.jsonString = gson.toJson(jarray) ;
		System.out.println(jsonString) ;
		return SUCCESS ;
	}
	public String delete() {  //删除
		System.out.println(stationID);
		try{
			stationService.removeService(stationID) ;
			this.jsonString="{success:true}" ;
		}catch(Exception e){
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		
		return SUCCESS ;
	}
	public String get()	{  //查看
		try{
			Station s = stationService.loadService(stationID) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("stationID", s.getStationID()) ;
			jobj.addProperty("stationCode", s.getStationCode()) ;
			jobj.addProperty("stationName", s.getStationName()) ;
			jobj.addProperty("remarks", s.getRemarks()) ;
			jobj.addProperty("success", true) ;
			this.jsonString = gson.toJson(jobj) ;
			System.out.println(jsonString) ;
		}catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String update() {
		try{
			System.out.println(station.getStationID()) ;
			stationService.updateService(station) ;
			this.jsonString = "{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
}
